﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.ComponentModel.Design;
using System.Data;
using System.Data.SqlClient;
public partial class gendan_auto : System.Web.UI.Page
{
    protected Model.CMember loginUserInfo = null;
    protected List<Model.LotteryChipped> allScheme = null;
    protected List<Model.Lottery> arlist = null;
    protected DataTable FellowUpRedDt = null;
    protected DataTable Winning = null;
    protected DataTable zt = null;
    protected string photoPath = "";
    protected string photo = "";
    protected DataTable dzrss = null;
    public int ID = ReadRequest.RequestInt("id");
    protected DataTable allmoney = null;
    protected List<Model.LotteryChipped> oneLotteryChipper = null;
    protected static long userid = 0;
  
    protected void Page_Load(object sender, EventArgs e)
        {

     
        FollowUpRedperson();//跟单红人
        Win();
        if (!IsPostBack)
        {
            int totalCount2 = 0;
            arlist = BLL.Lottery.GetPageList_Where(0, int.MaxValue, 0, out totalCount2);
            this.qsddl.DataSource = arlist;
            this.qsddl.DataValueField = "id";
            this.qsddl.DataTextField = "Name";
            this.qsddl.DataBind();
            qsddl.Items.Insert(0, new ListItem("--请选择彩种--", "0"));

            bindtable();
        }
      
       

        }
    /// <summary>
    /// 彩票种类
    /// </summary>
   
    private void FollowUpRedperson()//跟单红人
    {


        string sqlstr = @"select t1.*,t2.LotteryType from (
                select  top(3) UserID,num as GenDanCount,nickname,LotteryOpenTime,photo,winTotal,ProjectStatus,modellevel ,State,max(a.LotteryChippedID) as LotteryChippedID
	            from  (select[LotteryChippedID], count([LotteryChippedID]) as num from [dbo].LotteryChippedUser group by [LotteryChippedID]) a
	            left join[LotteryChipped] b on a.LotteryChippedID = b.id 
	            left join CMember c on b.UserID = c.Id 
	            left join UsersAccount d on c.id = d.user_id 
		                group by  UserID,nickname,LotteryOpenTime,photo,winTotal,ProjectStatus,modellevel,State,num order by GenDanCount desc
                ) t1
                left join [LotteryChipped] t2 on t1.LotteryChippedID=t2.id ";
        //order by [AddTime]   where t2.ProjectStatus = 2
        FellowUpRedDt = BLL.LotteryChippedUser.GetPageListBysql(sqlstr);

    }
  

    private void Win()  //中大奖
    {
        string sqlstr = @"select t1.*,t2.LotteryType from (
                select  top(3) UserID,num as GenDanCount,nickname,LotteryOpenTime,photo,winTotal,State,ProjectStatus,modellevel ,max(a.LotteryChippedID) as LotteryChippedID
	            from  (select[LotteryChippedID], count([LotteryChippedID]) as num from [dbo].LotteryChippedUser group by[LotteryChippedID]) a
	            left join[LotteryChipped] b on a.LotteryChippedID = b.id 
	            left join CMember c on b.UserID = c.Id 
	            left join UsersAccount d on c.id = d.user_id 
		                group by  UserID,nickname,LotteryOpenTime,photo,winTotal,ProjectStatus,modellevel,State,num order by winTotal desc
                ) t1
                left join [LotteryChipped] t2 on t1.LotteryChippedID=t2.id";

        Winning = BLL.LotteryChippedUser.GetPageListBysql(sqlstr);  // 之前用LotteryChippedUser

    }

    /// <summary>
    /// 根据用户名获取用户Id
    /// </summary>
    /// <param name="Uname"></param>
    /// <returns></returns>
    public long SelectUId(string Uname)
    {
        Model.CMember cm = BLL.CMember.GetOneBynickname(Uname);
        if (cm != null)
        {
            return cm.Id;//用户Id
        }
        else
        {
            return -1;
        }
    }

    

    public string SelectName(object Uid)
    {
        Model.CMember cm = BLL.CMember.GetModel(Convert.ToInt32(Uid));
        if (cm != null)
        {
            return cm.nickname;//用户真实姓名
        }
        else
        {
            return "";
        }
    }

    public Model.LotteryChipped LotteryChippedData(long UserID)
    {
        string sqlstr = "select max(ID) from LotteryChipped where UserID=" + UserID + "";
        long id = Convert.ToInt64(BLL.LotteryChipped.GetPageListBysql(sqlstr).Rows[0][1]);
        Model.LotteryChipped LC = BLL.LotteryChipped.GetModel(id);
        return LC;
    }
    //public string ShanChang(long UserID)
    //{
    //    string sqlstr = "select top 1 sum(LotteryWinMoney) as allmoney, [LotteryType]  from LotteryChipped where UserID=" + UserID + "  group by [LotteryType] order by allmoney desc";
    //    string ShanChangType = BLL.LotteryChippedUser.GetPageListBysql(sqlstr).Rows[0][1].ToString();
    //    return ShanChangType;
    //}
    private void bindtable()
    {
        PagedDataSource pds = new PagedDataSource();
        string sql = "";
        string NickName = UserName.Text;
        if (UserName.Text == "" || UserName.Text == null)
        {
            if (int.Parse(qsddl.SelectedValue.ToString()) > 0)
            {
                sql = " and LotteryType=" + int.Parse(qsddl.SelectedValue.ToString());
            }
            if (int.Parse(isManDDl.SelectedValue.ToString()) == 0)
            {
                sql += " and (LotteryJD <=100)";
            }
            else if (int.Parse(isManDDl.SelectedValue.ToString()) == 1)
            {
                sql += " and LotteryJD <100";
            }
            else if (int.Parse(isManDDl.SelectedValue.ToString()) == 2)
            {
                sql += " and (LotteryJD between 50 and 100)";
            }
            else if (int.Parse(isManDDl.SelectedValue.ToString()) == 3)
            {
                sql += " and (LotteryJD >=90)";
            }
         
        }
        else
        {
            long UId = SelectUId(NickName);

            sql = "and UserID=" + UId;
        }
        //LotteryChipped.GetPageListBysqlsql//新加的一个方法
        int totalCount4 = 0;
        allScheme = BLL.LotteryChipped.GetPageListBysqlsql(this.AspNetPager1.CurrentPageIndex - 1, this.AspNetPager1.PageSize, sql, out totalCount4);
        if (allScheme != null && allScheme.Count > 0)
        {
            AspNetPager1.RecordCount = totalCount4;
            pds.AllowPaging = true;
            pds.PageSize = AspNetPager1.PageSize;
            pds.CurrentPageIndex = AspNetPager1.CurrentPageIndex - 1;

        }



    }
 

    public int chakan_count(int Id)
    {
        int chakanCount = BLL.LotteryChippedUser.GetAll_Count(Id);
        return chakanCount;
    }

    public int dzrs_count(int Id)
    {
        int dzrscount = BLL.LotteryChippedUser.Rs_Count(Id);
        return dzrscount;
    }

    public string ViewModelLevel(long UserId)
    {

        string divs = BLL.LotteryChipped.ViewModelLevel(UserId);
        return divs;

    }

    protected void qsddl_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindtable();
    }

    protected void isManDDl_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindtable();
    }

    protected void gendan_search_Click(object sender, EventArgs e)
    {
        bindtable();
    }

    protected void AspNetPager1_PageChanged(object sender, EventArgs e)
    {
        bindtable();
    }
}